<template>
  <view class="w-full max-w-[750rpx] mx-auto min-h-screen bg-[#f6f8fa] p-[32rpx] box-border">
    <text class="block text-[48rpx] font-extrabold mb-[40rpx] text-center text-blue-600 drop-shadow">新增监护人信息</text>
    <FormView :fieldConfig="fieldConfig" :form="form" :showButtons="true" @submit="submitForm" />
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import Taro from '@tarojs/taro'
import { guardianInfoFields as fieldConfig } from './fields'
import FormView from '@/components/FormView.vue'
import { request } from '@/service/request'

onMounted(async () => {
  // 可以在这里加载一些初始化数据
  console.log('新增监护人信息页面加载')
  // 初始化表单数据
  const obj = Taro.getStorageSync('currentUser')
  if(obj){
    form.name = obj.name;
    form.idNumber = obj.idNumber;
    form.recordid = obj.recordid;
  }
})

const form = reactive<any>({})

async function submitForm(form: any, fieldConfig: any) {
  try {
    // 提交数据
    const params = {
      ...form,
    }
    
    const result = await request.post<any>('/guardian/guardianInfo/add', params, {
      useErrMsg: true
    })
    
    if (result) {
      Taro.showToast({ title: '保存成功', icon: 'success' })
      // 返回上一页
      setTimeout(() => {
        Taro.navigateBack()
      }, 1500)
    }
  } catch (error) {
    console.error('保存失败:', error)
    Taro.showToast({ title: '保存失败', icon: 'error' })
  }
}
</script> 